using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class JobSeekers_login : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session.Contents["rcuserid"] != null)
        {
            Response.Redirect("myaccount.aspx");
        }
        if (Request.Cookies["Remember"] != null)
        {
            HttpCookie httpCookie =
            Request.Cookies["RCEmailAndTime"];
            txtEmail.Text = httpCookie.Values["RCEmail"].ToString();

        }
    }
    protected void btnLogin_Click(object sender, EventArgs e)
    {
        string[] valueToGet = new string[3] { "", "", "" };
        SqlServerProvider cls = new SqlServerProvider();
        
        valueToGet=cls.LoginAuthentication(txtEmail.Text, txtPassword.Text, "1");
        if (valueToGet[0]== "OK")
        {
            Session.Add("rcuserid", valueToGet[1]);
            Session.Add("rcemail", txtEmail.Text);
            Session.Add("rcfullname", valueToGet[2]);
            WriteCookie(txtEmail.Text);
            if(Session["rcCurrentPage"] != null)
                Response.Redirect(Session["rcCurrentPage"].ToString()); 
            else
                Response.Redirect("myaccount.aspx");
        }
        else
        {
            switch(valueToGet[0])
            {
                case "U":
                    lblHeader.Text="Email is not existing,"+
                    " please re-enter your email and try again.";
                    break;
                case "P":
                    lblHeader.Text = "Invalid password, "+
                    "please re-enter your own password and try again.";
                    break;
                default:
                    lblHeader.Text = "System error, " + cls.ErrorMessage;
                    break;
            }
        }
    }
    void WriteCookie(string email)
    {
        if (CheckBoxRememberEmail.Checked)
        {
            HttpCookie httpCookie = new HttpCookie("RCRemember", "Yes");
            Response.Cookies.Add(httpCookie);
            httpCookie = new HttpCookie("RCEmailAndTime");
            httpCookie.Values.Add("RCEmail",email);
            httpCookie.Values.Add("RCTime", DateTime.Now.ToShortDateString());
            Response.Cookies.Add(httpCookie);
        }
    }
}
